package com.example.notepad.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.notepad.Bean.Content;
import com.example.notepad.Layout.RvSlideLayout;
import com.example.notepad.R;
import com.example.notepad.Utils.MyHolderView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;



public class NoteAdapter extends RecyclerView.Adapter<MyHolderView> {
    private List<Content> mylist;
    private Context context;
    private RvSlideLayout mOpenMenu;
    private RvSlideLayout mScrollingMenu;

    public NoteAdapter(List<Content> mylist, Context context) {
        this.mylist = mylist;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolderView onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.notepad_item_layout,viewGroup,false);

        return new MyHolderView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolderView myHolderView, @SuppressLint("RecyclerView") final int i) {
        String mTitle;
        String util1 = "<font color='#E6E6E6'><small>";
        String util2 = "</small></font>";
        if(mylist.get(i).getTitle().length() > 10) {
            mTitle = mylist.get(i).getTitle().substring(0,10) + "...";
        } else if(mylist.get(i).getTitle().length() == 0) {
            if(mylist.get(i).getContent().length() > 10) {
                mTitle = mylist.get(i).getContent().substring(0,10) + "...";
            } else {
                mTitle = mylist.get(i).getContent();
            }
        } else {
            mTitle = mylist.get(i).getTitle();
        }
        mTitle = mTitle + "<br />";
        myHolderView.title.setText(Html.fromHtml(mTitle + util1 +
                mylist.get(i).getUpdatedAt().substring(5, 7) + "月" +
                mylist.get(i).getUpdatedAt().substring(8, 10) + "日 " +
                mylist.get(i).getUpdatedAt().substring(11, 16) +
                util2));
        myHolderView.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String objectId = mylist.get(i).getObjectId();
                Intent intent = new Intent(context, com.example.notepad.Activity.ContentActivity.class);
                intent.putExtra("id",objectId);
                context.startActivity(intent);
            }
        });
        /*myHolderView.title.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                return false;
            }
        });*///长按置顶，还未实现
        myHolderView.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String objectId = mylist.get(i).getObjectId();
                Content mycontent = new Content();
                mycontent.delete(objectId, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            Toast.makeText(context,"删除成功",Toast.LENGTH_LONG).show();
                        }
                    }
                });
                mylist.remove(i);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mylist.size();
    }

    public void setScrollingMenu(RvSlideLayout scrollingMenu) {
        mScrollingMenu = scrollingMenu;
    }

    public RvSlideLayout getScrollingMenu() {
        return mScrollingMenu;
    }

    public void closeOpenMenu() {
        if (mOpenMenu != null && mOpenMenu.isOpen()) {
            mOpenMenu.closeMenu();
            mOpenMenu = null;
        }
    }

    public void holdOpenMenu(RvSlideLayout rvSlideLayout) {
        mOpenMenu = rvSlideLayout;
    }

    public void setAdd(List<Content> list) {
        mylist.clear();
        this.mylist = list;
        notifyDataSetChanged();
    }

}